package com.imufe.chemicalenterprisedangerwarning.common;

import lombok.Data;

import java.io.Serializable;

/**
 * <p>统一返回的数据格式</p>
 * <a href="https://github.com/ecomfe/spec/blob/master/e-json.md">参考原文（百度前端规范）</a>
 * @author BaiBan
 * @since 2022/7/10
 */
@Data
public class ResponseEntity<T> implements Serializable {

    private String code;
    private String message;
    private T data;

    /**
     * <p>请求处理成功时使用的工具方法</p>
     */
    public static <Type> ResponseEntity<Type> success() {
        return new ResponseEntity<>(InfoCode.SUCCESS);
    }

    public static <Type> ResponseEntity<Type> success(Type data) {
        return new ResponseEntity<>(InfoCode.SUCCESS, data);
    }

    /**
     * <p>请求处理失败时使用的工具方法</p>
     */
    public static <Type> ResponseEntity<Type> fail() {
        return new ResponseEntity<>(InfoCode.FAIL);
    }

    public static <Type> ResponseEntity<Type> fail(InfoCode code) {
        return new ResponseEntity<>(code);
    }

    public static ResponseEntity<Object> fail(String code, String message) {
        return new ResponseEntity<>(code, message);
    }

    public ResponseEntity(InfoCode code) {
        this.code = code.getCode();
        this.message = code.getMessage();
    }

    public ResponseEntity(InfoCode code, T data) {
        this.code = code.getCode();
        this.message = code.getMessage();
        this.data = data;
    }

    public ResponseEntity(String code, String message) {
        this.code = code;
        this.message = message;
    }
}
